﻿import QtQuick 2.0
import Box2D 2.0
import QtQuick.Window 2.2
import "./shared"
import "./tree"

Item {
    id: screen
    width: Screen.width
    height: Screen.height

//    Rectangle{
//        width: 100
//        height: 200
//    }

    property real power: 600
    property int level: 5
    property int leftScore: 0
    property int rightScore: 0
    property bool toDir: true   //you

    Keys.enabled: true
    focus: true

    Keys.onPressed: {
        if (event.key == Qt.Key_Escape) {
            console.log(12)
            menu.visible = true
            event.accepted = true;
        }
    }

    function resume(){

    }
    function restartIn(){
        leftbox.x = 20
        leftbox.y = (screen.height - leftbox.height)/2

        rightbox.x = screen.width - rightbox.width - 20
        rightbox.y = (screen.height - rightbox.height)/2

        ball.body.linearVelocity = Qt.point(0,0)
        ball.body.angularVelocity = 0

        ball.x = (screen.width - ball.width)/2
        ball.y = (screen.height - ball.height)/2
        ball.body.active = true

        var dir = Math.floor(Math.random() * 10) % 2 == 0
        toDir = dir
        ball.body.applyLinearImpulse(Qt.point(dir?power:-power,power),ball.body.getWorldCenter())
        ball.body.angularVelocity = 50

        ball.visible = true
    }
    function exit(){
        widget.systemExit(true)
    }

    function restart(){
        leftScore = 0
        rightScore = 0
        restartIn()
    }

    function calcAI(){
        if(ball.x < screen.width/2 && !toDir){
            var value = ball.y + ball.height/2 - leftbox.height/2
            if(value < 0){
                leftbox.y = 0
            }
            else if(value > screen.height - leftbox.height){
                leftbox.y = screen.height - leftbox.height
            }
            else{
                leftbox.y = value
            }
        }

        //You ai
//        if(ball.x > screen.width/2 && toDir){
//            var value = ball.y + ball.height/2 - leftbox.height/2
//            if(value < 0){
//                rightbox.y = 0
//            }
//            else if(value > screen.height - rightbox.height){
//                rightbox.y = screen.height - rightbox.height
//            }
//            else{
//                rightbox.y = value
//            }
//        }
    }

    Component.onCompleted: {
        restart()
    }

    Timer{
        id: timer
        interval: 50
        repeat: true
        running: true
        onTriggered: {
            calcAI()
        }
    }
    Timer{
        id: timerRestart
        interval: 2000
        repeat: false
        onTriggered: {
            restartIn()
        }
    }
    Timer{
        id: timerDispear
        interval: 1000
        repeat: false
        onTriggered: {
            ball.visible = false
        }
    }

    Tree{
        id: tree
        anchors.centerIn: parent
        anchors.horizontalCenterOffset: -180
        anchors.verticalCenterOffset: -300
        hz: 0
    }

    Menu{
        id: menu
        z: 10
        visible: false
    }

    World {id: physicsWorld}

    ImageBoxBody {
        id: leftbox
        smooth: true
        objectName: "leftbox"
        source: "qrc:/imamges/image/left.png"

        bodyType: Body.Static
        world: physicsWorld

        density: 1
        friction: 0.5
        restitution: 1
        gravityScale: 0

        Behavior on y {
            NumberAnimation{duration: Math.floor(1000 / level)}
        }
    }

    ImageBoxBody {
        id: rightbox
        smooth: true
        objectName: "rightbox"
        source: "qrc:/imamges/image/left.png"

        bodyType: Body.Static
        world: physicsWorld

        density: 1
        friction: 0.5
        restitution: 1
        gravityScale: 0

//        Behavior on y {
//            NumberAnimation{duration: Math.floor(1000 / level)}
//        }
    }

    MouseArea{
        anchors.fill: rightbox
        drag.target: rightbox
        drag.axis: Drag.YAxis
        drag.minimumY: 0
        drag.maximumY: screen.height-rightbox.height
    }

    ImageCircleBody {
        id: ball
        source: "qrc:/imamges/image/ball.png"
        x: (screen.width - ball.width)/2
        y: (screen.height - ball.height)/2

        bodyType: Body.Dynamic
        world: physicsWorld
        smooth: true

        density: 1
        friction: 0.0
        restitution: 1
        gravityScale: 0

        onXChanged: {
            if(x < -ball.width){
                //
                rightScore++
                ball.body.active = false
                timerDispear.restart()
                timerRestart.restart()
            }
            else if(x > screen.width + ball.width){
                leftScore++
                ball.body.active = false
                timerDispear.restart()
                timerRestart.restart()
            }
        }

        onBeginContact: {
//            console.log("---------",other.getBody().objectName,"====",other.getBody().target.objectName)
            if(other.getBody().objectName == "right" && ball.visible){
                leftScore++
                timerRestart.restart()
            }
            else if(other.getBody().objectName == "left" && ball.visible){
                rightScore++
                timerRestart.restart()
            }
            else if(other.getBody().target.objectName == "leftbox" && ball.visible){
                ball.body.applyLinearImpulse(Qt.point(300,100),ball.body.getWorldCenter())
                ball.body.angularVelocity += 50*Math.random()
                toDir = true
            }
            else if(other.getBody().target.objectName == "rightbox" && ball.visible){
                ball.body.applyLinearImpulse(Qt.point(-300,100),ball.body.getWorldCenter())
                ball.body.angularVelocity += 50*Math.random()
                toDir = false
            }
        }
    }

    ScreenBoundaries {}

    // Debug
    DebugDraw {
        id: debugDraw
        world: physicsWorld
        opacity: 0.75
        visible: false
    }
}
